import { _decorator, Component, Node, Label } from 'cc';
import { BasicUI } from '../../Init/Basic/BasicUI';
import { GlobalConfig } from '../../Init/Config/GlobalConfig';
import { GlobalTmpData } from '../../Init/Config/GlobalTmpData';
import { EventTypes } from '../../Init/Managers/EventTypes';
import { AudioEnum } from '../../Init/SystemAudio/AudioEnum';
import { AudioSystem } from '../../Init/SystemAudio/AudioSystem';
import { StorageSystem } from '../../Init/SystemStorage/StorageSystem';
import { UIEnum } from '../../Init/SystemUI/UIEnum';
import { UISystem } from '../../Init/SystemUI/UISystem';
import { Adsdk } from '../../myScript/Adsdk';
const { ccclass, property } = _decorator;

@ccclass('ResurgenceLvupUI')
export class ResurgenceLvupUI extends BasicUI {

    show(d?) {
        super.show();
        this.setProp();

    }

    @property(Label)
    spdProp: Label = null;
    @property(Label)
    hpProp: Label = null;
    @property(Label)
    numProp: Label = null;

    @property(Node)
    costBtn: Node = null;
    @property(Node)
    costVideoBtn: Node = null;
    @property(Node)
    closeBtn: Node = null;

    @property(Label)
    costLabel: Label = null;

    _cost = 1500;
    _isSpdMax = false;
    _isNumMax = false;

    setProp() {
        this.costLabel.string = this._cost.toFixed(0);

        const _cfg = GlobalConfig.RolePropCfg;
        let d = StorageSystem.getData();
        //HP
        this.hpProp.string = Math.floor((d.userAssets.props.hpLv) * 10 + 100).toFixed(0) + '%';
        //SPD
        let spdNum = _cfg.getMoveSpdByLv(d.userAssets.props.moveSpdLv);
        this.spdProp.string = Math.floor((d.userAssets.props.moveSpdLv - 1) * 10 + 100).toFixed(0) + '%';
        this._isSpdMax = spdNum >= _cfg.moveSpd.max;
        if (this._isSpdMax) {
            this.spdProp.string = '已达上限';
        }
        //Num
        this.numProp.string = Math.floor(d.userAssets.props.numLv * 100).toFixed(0) + '%';
        let num = _cfg.getNumRateByLv(d.userAssets.props.numLv);
        this._isNumMax = num >= _cfg.num.max;
        if (this._isNumMax) {
            this.numProp.string = '已达上限';
        }
        this.closeBtn.active = true;
        this.costBtn.active = true;
        this.costVideoBtn.active = false;

        if (GlobalTmpData.isMiscontact) {
            this.costBtn.active = d.userAssets.asset >= this._cost;
            this.costVideoBtn.active = !(d.userAssets.asset >= this._cost);
            this.closeBtn.active = false;
        }
    }
    addNum = 0;

    hide() {
        super.hide();

    }

    public saveAward() {
        StorageSystem.setData((d) => {
            d.userAssets.props.hpLv++;
            if (!this._isSpdMax) {
                d.userAssets.props.moveSpdLv++;
            }
            if (!this._isNumMax) {
                d.userAssets.props.numLv++;
            }
        }, true);
        this.emit(EventTypes.RoleEvents.Resurgence);
    }

    public onVideoClick() {
        this.emit(EventTypes.SDKEvents.ShowVideo, () => {
            //
            this.saveAward();
            UISystem.hideUI(UIEnum.ResurgenceLvupUI);
        })
    }

    public onCostClick() {
        let d = StorageSystem.getData();
        if (d.userAssets.asset < this._cost) {
            this.emit(EventTypes.GameEvents.ShowTips, '金币不足!');
            UISystem.showUI(UIEnum.FreeGoldUI);
            return;
        } else {
            d.userAssets.asset -= this._cost;
            StorageSystem.saveData();
            StorageSystem.updateToAssets();
            this.saveAward();
            UISystem.hideUI(UIEnum.ResurgenceLvupUI);
        }
    }

    // 关闭
    public onCloseClick() {
        AudioSystem.playEffect(AudioEnum.BtnClick);
        this.emit(EventTypes.RoleEvents.CanceResurgence);
        UISystem.hideUI(UIEnum.ResurgenceLvupUI);
        this.emit(EventTypes.RoleEvents.CanceResurgence);

    }

}

